﻿using System;
using System.Drawing;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.GoodsModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmGoodsSale : Form
    {
        public FrmGoodsSale()
        {
            InitializeComponent();
        }

        string name;
        decimal inPrice;
        decimal outPrice;
        int inventory;
        decimal totalPrice;
        decimal backPrice;

        private void FrmGoodsSale_Load(object sender, EventArgs e)
        {
            txtDate.Text = DateTime.Now.ToLongDateString();
            txtSeller.Text = UserManager.userInfo[0];
            btnOk.Enabled = false;
            if (UserManager.loginType == "SaleLogin")
            {
                this.WindowState = FormWindowState.Maximized;
                Point p1 = new Point();
                p1.X = this.Width / 2 - 302;
                p1.Y = (this.Height - 480) / 2;
                gbGoods.Location = p1;
                Point p2 = new Point();
                p2.X = this.Width / 2 - 302;
                p2.Y = (this.Height - 480) / 2 + 114;
                gbPay.Location = p2;
                Point p3 = new Point();
                p3.X = this.Width / 2 - 302;
                p3.Y = (this.Height - 480) / 2 + 305;
                gbElse.Location = p3;
                btnChangeUser.Enabled = true;
            }
            else
            {
                btnChangeUser.Enabled = false;
            }
        }

        private void FrmGoodsSale_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                Point p1 = new Point();
                p1.X = this.Width / 2 - 302;
                p1.Y = (this.Height - 480) / 2;
                gbGoods.Location = p1;
                Point p2 = new Point();
                p2.X = this.Width / 2 - 302;
                p2.Y = (this.Height - 480) / 2 + 114;
                gbPay.Location = p2;
                Point p3 = new Point();
                p3.X = this.Width / 2 - 302;
                p3.Y = (this.Height - 480) / 2 + 305;
                gbElse.Location = p3;
            }
            else
            {
                Point p1 = new Point(9, 7);
                gbGoods.Location = p1;
                Point p2 = new Point(9, 121);
                gbPay.Location = p2;
                Point p3 = new Point(9, 312);
                gbElse.Location = p3;
            }
        }

        private void FrmGoodsSale_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (UserManager.loginType == "SaleLogin")
            {
                Application.Exit();
            }
        }

        private void txtGoodsID_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    string goodsID = txtGoodsID.Text.Trim();
                    if (goodsID == "")
                    {
                        MessageBox.Show("商品号不能为空", "提示");
                        txtGoodsID.Focus();
                        return;
                    }
                    GoodsManager goodsManager = new GoodsManager();
                    Goods goods = goodsManager.GetGoods(goodsID);
                    if (goods == null)
                    {
                        MessageBox.Show("输入的商品号不存在", "提示");
                        txtGoodsID.SelectAll();
                        txtGoodsID.Focus();
                        return;
                    }
                    name = goods.Name;
                    inPrice = goods.InPrice;
                    outPrice = goods.OutPrice;
                    inventory = goods.Inventory;
                    txtName.Text = name;
                    txtOutPrice.Text = outPrice.ToString();
                    txtInventory.Text = inventory.ToString();
                    txtQuantity.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtQuantity_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    if (txtQuantity.Text.Trim() == "")
                    {
                        MessageBox.Show("购买数量不能为空", "提示");
                        txtQuantity.Focus();
                        return;
                    }
                    int quantity = int.Parse(txtQuantity.Text.Trim());
                    if (quantity <= 0)
                    {
                        MessageBox.Show("购买数量必须是大于零的整数", "提示");
                        txtQuantity.SelectAll();
                        txtQuantity.Focus();
                        return;
                    }
                    if (quantity > inventory)
                    {
                        MessageBox.Show("库存不足，该商品仅剩" + inventory + "件", "提示");
                        txtQuantity.SelectAll();
                        txtQuantity.Focus();
                        return;
                    }
                    outPrice = decimal.Parse(txtOutPrice.Text.Trim());
                    totalPrice = outPrice * quantity;
                    txtTotalPrice.Text = totalPrice.ToString();
                    txtActuaPrice.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtActuaPrice_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    if (txtActuaPrice.Text.Trim() == "")
                    {
                        MessageBox.Show("实收金额不能为空", "提示");
                        txtActuaPrice.Focus();
                        return;
                    }
                    int actualPrice = int.Parse(txtActuaPrice.Text.Trim());
                    if (actualPrice < totalPrice)
                    {
                        MessageBox.Show("实收金额必须不能小于应收金额", "提示");
                        txtActuaPrice.SelectAll();
                        txtActuaPrice.Focus();
                        return;
                    }
                    totalPrice = decimal.Parse(txtTotalPrice.Text.Trim());
                    backPrice = actualPrice - totalPrice;
                    txtBackPrice.Text = backPrice.ToString();
                    btnOk.Enabled = true;
                    btnOk.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                Sale sale = new Sale();
                sale.GoodsID = txtGoodsID.Text.Trim();
                sale.Quantity = int.Parse(txtQuantity.Text.Trim());
                sale.TotalPrice = decimal.Parse(txtTotalPrice.Text.Trim());
                sale.Earnings = (totalPrice - sale.Quantity * inPrice);
                sale.Date = txtDate.Text.Trim();
                sale.Seller = txtSeller.Text.Trim();
                int inventory = this.inventory - sale.Quantity;
                PublicMethodsManager publicMethodsManager = new PublicMethodsManager();
                if (publicMethodsManager.AddSaleDataChangeInventory(sale, inventory))
                {
                    txtGoodsID.Text = "";
                    txtName.Text = "";
                    txtQuantity.Text = "";
                    txtOutPrice.Text = "";
                    txtTotalPrice.Text = "";
                    txtActuaPrice.Text = "";
                    txtBackPrice.Text = "";
                    txtName.Text = "";
                    MessageBox.Show("付款成功", "提示");
                    btnOk.Enabled = false;
                    txtGoodsID.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (UserManager.loginType == "SaleLogin")
            {
                Application.Exit();
            }
            else
            {
                this.Close();
            }
        }

        private void btnChangeUser_Click(object sender, EventArgs e)
        {
            FrmLogin ob_FrmLogin = new FrmLogin();
            ob_FrmLogin.Show();
            this.Hide();
        }
    }
}
